package com.mida.app.knw.service.impl;


import com.mida.app.knw.service.MdmService;
import com.mida.app.knw.service.DtUseSpaceService;
import com.mida.dms.knw.api.DtUseSpaceApi;
import com.mida.dms.knw.entity.DtUseSpace;
import com.mida.dms.knw.entity.vo.DtUseSpaceVo;
import com.mida.dms.knw.vo.DtUseSpacePageVo;
import com.mida.dms.knw.vo.PageResultVo;
import com.mida.dms.mdm.vo.provider.BaseOrgInfoVo;
import com.mida.dms.mdm.vo.provider.BusinessUnitVo;
import com.mida.dms.mdm.vo.provider.EmployeeInfoVo;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static com.mida.tool.common.result.BaseResult.buildListWarn;

/**
 * @author ：wang xiao le
 * @description：TODO
 * @date ：2021/3/23 17:30
 */
@Service
@Slf4j
public class DtUseSpaceServiceImpl implements DtUseSpaceService {
    @Autowired
    private DtUseSpaceApi dtUseSpaceApi;
    @Autowired
    private MdmService mdmService;

    @Override
    public ActionResult add(DtUseSpaceVo dtUseSpaceVo) {
        return dtUseSpaceApi.add(dtUseSpaceVo);
    }

    @Override
    public ActionResult update(DtUseSpaceVo dtUseSpaceVo) {
        return dtUseSpaceApi.update(dtUseSpaceVo);
    }

    @Override
    public ActionResult delete(long useSpaceSoid) {
        return dtUseSpaceApi.delete(useSpaceSoid);
    }

    @Override
    public ListResult<DtUseSpaceVo> get(long useSpaceSoid) {
        ListResult<DtUseSpaceVo> lr = new ListResult<DtUseSpaceVo>();
        ListResult<DtUseSpaceVo> dtUseSpaceVoListResult = null;
        try {
            dtUseSpaceVoListResult = dtUseSpaceApi.get(useSpaceSoid);
            if(ObjectUtils.isNotEmpty(dtUseSpaceVoListResult)&&ObjectUtils.isNotEmpty(dtUseSpaceVoListResult.getData())){
                DtUseSpaceVo dtUseSpaceVo = dtUseSpaceVoListResult.getData();
                //回显医院名称
                if (dtUseSpaceVo.getHospitalSoid()!=null) {
                    ListResult<BaseOrgInfoVo> orgByOrgInfoResult = mdmService.findOrgByOrgInfoSoid(dtUseSpaceVo.getHospitalSoid(), false);
                    if (orgByOrgInfoResult.getData()!=null) {
                        dtUseSpaceVo.setHospitalName(orgByOrgInfoResult.getData().getOrgInfoName());
                    }
                }
                //回显业务单元名称
                Optional.ofNullable(dtUseSpaceVo.getDtUseSpaceUnitList()).orElse(new ArrayList<>()).forEach(vo -> {
                    if (vo.getBusinessUnitSoid() != null) {
                        ListResult<BusinessUnitVo> businessUnit = mdmService.findBusinessUnitBySoid(vo.getBusinessUnitSoid(), false);
                        if (businessUnit.getData()!=null) {
                            vo.setUnitName(businessUnit.getData().getBusinessUnitName());
                        }
                    }
                });
                //回显授权人员名称
                Optional.ofNullable(dtUseSpaceVo.getDtUseSpaceConsumerList()).orElse(new ArrayList<>()).forEach(vo -> {
                    if (vo.getDoctorSoid() != null) {
                        ListResult<EmployeeInfoVo> employeeInfoVoListResult = mdmService.findOneBySoid(vo.getDoctorSoid(), false);
                        if (employeeInfoVoListResult.getData()!=null) {
                            vo.setDoctorName(employeeInfoVoListResult.getData().getEmployeeName());
                        }
                    }
                });
            }
        } catch (Exception e) {
            log.error("获取单个定义空间发送异常", e);
            buildListWarn(lr, "获取单个定义空间失败");
        }
        return dtUseSpaceVoListResult;
    }

    @Override
    public ListResult<PageResultVo<DtUseSpaceVo>> findPage(DtUseSpacePageVo vo) {
        return dtUseSpaceApi.findPage(vo);
    }

    @Override

    public ActionResult checkName(String name, Long useSpaceSoid) {
        return dtUseSpaceApi.checkName(name,useSpaceSoid);
    }

    @Override
    public ActionResult updateStatus(Long status, Long useSpaceSoid) {
        return dtUseSpaceApi.updateStatus(status,useSpaceSoid);
    }

    @Override
    public ListResult<List<DtUseSpace>> findAll() {
        return dtUseSpaceApi.findAll();
    }
}
